//
//  BoyeViewHandle.m
//  itboye2015002
//
//  Created by boye_mac1 on 15/8/18.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "BoyeViewHandle.h"

@implementation BoyeViewHandle

#pragma mark  动画
+(void) setViewFlipFromRightAnimation:(UIView * )view{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO];
    [UIView commitAnimations];
}

#pragma mark  Rect
+(void) setAnimationView:(UIView * )view duration:(CGFloat)duration rect:(CGRect) rect{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:duration];
    view.frame = rect;
    [UIView commitAnimations];
}

#pragma mark  PointCent
+(void) setAnimationCentView:(UIView * )view duration:(CGFloat)duration pointCent:(CGPoint) pointCent{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:duration];
    view.center = pointCent;
    [UIView commitAnimations];
    
}


#pragma mark  返回图片对象
+(UIImage *) getImageOfString:(NSString *)imagString{
    
    UIImage * image = [UIImage imageNamed:imagString];
    return image;
}


#pragma mark   清理特殊标签的子视图 ---

+(void)clearCellSonView:(UIView*)contentView viewTag:(NSInteger) tag {
    
    for (UIView * view in [contentView subviews]) {
        
        if (view.tag == tag) {
            [view removeFromSuperview];
        }
    }
}

#pragma mark   清理特殊标签的子视图 ---

+(UIView *)findSonView:(UIView*)contentView viewTag:(NSInteger) tag {
    UIView * view = nil;
    for (UIView * sonView in [contentView subviews]) {
    
        if (sonView.tag == tag) {
            view = sonView;
            break;
        }
    }
    return view;
}
#pragma mark -清理所有子视图
+(void)clearAllSonView:(UIView *)view{
    
    for (UIView * sonView in [view subviews]) {
        [sonView removeFromSuperview];
    }
}

#pragma mark -所有子视图是否可用
+(void)touchAbleSonView:(UIView *)view able:(BOOL)able{
    
    for (UIView * sonView in [view subviews]) {
        sonView.userInteractionEnabled = able;
    }
}


#pragma mark 切割圆角
+(void)cutViewConner:(UIView *)view radius:(CGFloat)radius{
    
    view.layer.cornerRadius = radius;
    view.layer.masksToBounds = YES;
    
}

#pragma mark -- 设置边框--
+(void)setBoardOfView:(UIView *) view :(CGFloat)width :(UIColor *)color{
    
    view.layer.borderColor = color.CGColor;
    view.layer.borderWidth = width;
}


#pragma mark -- 设置边框--
+(void) setBoardOfView:(UIView *) view :(CGFloat)width{
    
    [self setBoardOfView:view :1 :[UIColor colorWithHexString:@"#c7c7c7"]];
}


#pragma mark -- 创建一条线
+(UIView *)lineCreatedInView:(UIView *)view fram:(CGRect )rect color:(UIColor *) color{
    
    UILabel * label =  [[UILabel alloc] init];
    label.frame = rect;
    label.backgroundColor = color;
    [view addSubview:label];
    return label;
}


#pragma mark -- 创建一条线 (底部)
+(UIView *)defaultLineInView:(UIView *)view tag:(NSInteger)tag{
 
    UIView * line = [self lineCreatedInView:view
                                    fram:CGRectMake(0, view.frame.size.height -1, view.frame.size.width, 1)
                                   color:[UIColor colorWithHexString:@"#d7d7d7"]];
    line.tag = tag;
    return line;
}

#pragma mark -- 创建一条线
+(UIView *)defaultLineInView:(UIView *)view{

 return [self defaultLineInView:view tag:0];

}

#pragma mark --获得图片的尺寸 --
+(CGSize )getImageSizeUrlStr:(NSString *)imageurl{
    
    UIImage* image = [self getImageUrlStr:imageurl];
    return image.size;
}

#pragma mark --获得图片 --

+(UIImage *) getImageUrlStr:(NSString *)imageurl{
    
    if (imageurl == nil || [imageurl isKindOfClass:[NSNull class]]) {
        DLog(@" \r ** image is nil ");
        return nil;
    }
    
    NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageurl]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    return image;
}

#pragma  mark -- 修改图片尺寸 --
+(UIImage *) alertImageSize:(UIImage *)image newSize:(CGSize) newSize{
    
    UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    //image is the original UIImage
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
                                 float ovalHeight)
{
    float fw, fh;
    
    if (ovalWidth == 0 || ovalHeight == 0)
    {
        CGContextAddRect(context, rect);
        return;
    }
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM(context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth(rect) / ovalWidth;
    fh = CGRectGetHeight(rect) / ovalHeight;
    
    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
    
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}
+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r
{
    // the size of CGContextRef
    int w = size.width;
    int h = size.height;
    
    UIImage *img = image;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGRect rect = CGRectMake(0, 0, w, h);
    
    CGContextBeginPath(context);
    addRoundedRectToPath(context, rect, r, r);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    img = [UIImage imageWithCGImage:imageMasked];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageMasked);
    
    return img;
}

@end
